package com.tbsoft.ehr.action.sysdata;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.tbsoft.ehr.action.BaseAction;
import com.tbsoft.ehr.model.ServiceCustomer;
import com.tbsoft.ehr.service.serviceCustomer.ServiceCustomerService;

public class ServiceCustomerAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(ServiceCustomerAction.class);

	private List<ServiceCustomer> scLst;
	private ServiceCustomer sc;
	
	private ServiceCustomerService serviceCustomerService;

	public List<ServiceCustomer> getScLst() {
		return scLst;
	}

	public void setScLst(List<ServiceCustomer> scLst) {
		this.scLst = scLst;
	}

	public ServiceCustomer getSc() {
		return sc;
	}

	public void setSc(ServiceCustomer sc) {
		this.sc = sc;
	}

	public ServiceCustomerService getServiceCustomerService() {
		return serviceCustomerService;
	}

	public void setServiceCustomerService(
			ServiceCustomerService serviceCustomerService) {
		this.serviceCustomerService = serviceCustomerService;
	}

	public String browse() {
		this.scLst = this.serviceCustomerService.getServiceCustomers();
		return "browse";
	}
	
	public String save(){
		try{	
			if(this.sc.getId().length()==0){
				this.sc.setId(null);
				ServiceCustomer sc = this.serviceCustomerService.getServiceCustomerByName(this.sc.getName());
				if(sc!=null) {
					return errorReturn("该服务客户已存在，请检查");
				}
			}
			this.serviceCustomerService.addServiceCustomer(this.sc);
			Map<String,Object> appMap = ActionContext.getContext().getApplication();			
			List<ServiceCustomer> tmpLst = this.serviceCustomerService.getServiceCustomers();
			Collections.sort(tmpLst);
			appMap.put("SC_LIST", tmpLst);
			setRel("serviceCustomer_browse");
			return SUCCESS;
		} catch(Exception ex) {
			log.error("添加/更新服务客户失败",ex);
			return errorReturn("添加/更新服务客户失败:"+ex.getMessage());
		}	
	}
	
	public String delete(){
		if(this.sc!=null&&!this.sc.getId().isEmpty()){
			try{
				this.serviceCustomerService.deleteServiceCustomer(this.sc);
				Map<String,Object> appMap = ActionContext.getContext().getApplication();			
				List<ServiceCustomer> tmpLst = this.serviceCustomerService.getServiceCustomers();
				Collections.sort(tmpLst);
				appMap.put("SC_LIST", tmpLst);
				
				setRel("serviceCustomer_browse");
			} catch(Exception ex){
				log.error("执行逻辑删除时出错",ex);
				return errorReturn("执行逻辑删除失败:"+ex.getMessage());
			}
		} else {
			log.info("没有需要删除的对象");
		}
		return SUCCESS;
	}
	
	public String edit(){
		if(this.sc!=null&&this.sc.getId()!=null){
			this.sc = this.serviceCustomerService.getServiceCustomerById(this.sc.getId());
			return "update";
		}else{
			log.info("没有选中需要编辑的数据");
			return "browse";
		}
	}
}
